﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcDataTables;
using MvcDataTablesDemo.Models;

namespace MvcDataTablesDemo.Controllers
{
    public class DemoController : Controller
    {
        //
        // GET: /Demo/

        public ActionResult Index()
        {
            return View();
        }

        public DataTableResult<DirectoryInfo> GetDirectories(DataTableViewModel viewModel)
        {
            return new DataTableResult<DirectoryInfo>(this, new DirectoryInfo(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles)).GetDirectories(), viewModel);
        }

        public DataTableResult<DirectoryModel> GetDirectoryModels(DataTableViewModel viewModel)
        {
            return new DataTableResult<DirectoryModel>(this, new DirectoryInfo(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles)).GetDirectories()
                .Select(lmb => new DirectoryModel(lmb)), viewModel);
        }

        public DataTableResult<FileModel> GetExecutableFiles(DataTableViewModel viewModel, DirectoryModel directoryModel)
        {
            return new DataTableResult<FileModel>(this, new DirectoryInfo(directoryModel.FullName).GetFiles("*.exe")
                .Select(lmb => new FileModel(lmb)), viewModel);
        }

        public DataTableResult<FileInfo> GetOtherFiles(DataTableViewModel viewModel, DirectoryModel directoryModel)
        {
            return new DataTableResult<FileInfo>(this, new DirectoryInfo(directoryModel.FullName).GetFiles().Where(lmb => lmb.Extension != ".exe"), viewModel);
        }

        public string DeleteDirectoryModel(DirectoryModel directoryModel)
        {
            return string.Format("Deleted {0}.", directoryModel.Name);
        }

        public int AddDirectoryModel(DirectoryModel directoryModel)
        {
            return 0;
        }

        public int AddFileModel(FileModel fileModel)
        {
            return 0;
        }

        public string UpdateDirectoryModel(DirectoryModel directoryModel, string column, string value)
        {
            return string.Format("Changed DirectoryModel's {0} to \"{1}\" from \"{2}\".", column, value, directoryModel.Name);
        }
    }
}
